package com.ymatou.iapi.sellerquery.service;

import java.util.Iterator;

import net.sf.json.JSONArray;

import org.json.JSONObject;

import com.ymatou.query.seller.trading.api.parameter.GetOrderProductAmountInfosResponse;
import com.ymt.base.YmatouBaseCall;
import com.ymt.core.tool.FormatUtil;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;

/**
 * @date 2016-09-06
 * @desc 根据商品Id获取订单中商品数量信息
 * 
 * @author songyefeng
 *
 */
public class GetOrderProductAmountInfosCall extends YmatouBaseCall {
	private static String opurl = "api/Order/GetOrderProductAmountInfos";

	public GetOrderProductAmountInfosCall() {
		super(EnvSetup.getSystemUrl("sellerquery_iapi"), opurl, "POST", "JSON");
	}

	@Override
	public void addDefHeader() {
		client.addHeader("Accept",
				"text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}

	/**
	 * 获取Code
	 * 
	 * @return
	 */
	public String getCode() {
		String Code = null;
		try {
			Code = new JSONObject(getReturnData()).getString("code");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Code;
	}

	/**
	 * 获取BCode
	 * 
	 * @return
	 */
	public String getBCode() {
		String BCode = null;
		try {
			BCode = new JSONObject(getReturnData()).getString("bcode");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BCode;
	}

	/**
	 * 获取Msg
	 * 
	 * @return
	 */
	public String getMsg() {
		String BCode = null;
		try {
			BCode = new JSONObject(getReturnData()).getString("msg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return BCode;
	}

	/**
	 * 获取Data
	 * 
	 * @return
	 */
	public Object getData() {
		Object Data = null;
		try {
			Data = new JSONObject(getReturnData()).get("data");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Data;
	}

	@SuppressWarnings("unchecked")
	public GetOrderProductAmountInfosResponse getOrderProductAmountInfosResponse() {
		GetOrderProductAmountInfosResponse Data = null;
		try {
			String resp = DataUtil.Json.parseJsonObj(getData().toString())
					.toString();
			net.sf.json.JSONObject obj= net.sf.json.JSONObject.fromObject(resp);
			net.sf.json.JSONObject infos = obj.getJSONObject("AmountInfos");
			net.sf.json.JSONObject need = new net.sf.json.JSONObject();
			Iterator<String> it = infos.keys();
			while(it.hasNext()){
				String productId = it.next();
				need.put(DataUtil.toLowerCaseFirstOne(productId), infos.get(productId));
			}
			net.sf.json.JSONObject copy = obj;
			copy.remove("AmountInfos");
			copy.put("AmountInfos", need);
			
			Data = (GetOrderProductAmountInfosResponse) FormatUtil
					.GSONToObject(copy.toString(),
							GetOrderProductAmountInfosResponse.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Data;
	}

}
